package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;


public class My_dynamic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		String user_account = request.getParameter("user_account");  //获取我关注的人的动态
		if(user_account==null||user_account.equals("")){     //获取自己的动态
			user_account = request.getSession().getAttribute("user_account").toString();   //用户账号
		}
		String select_dynamic = "select * from dynamic_information where user_account = '"+user_account+"'";   //查询该用户发布的动态
		
		String dynamic_photo_path;   //发布动态的图片url
		String dynamic_information_content;   //动态的类容
		java.sql.Date dynamic_information_date;  //动态发布的时间
		String dynamic_information_id;   //动态的编号
		int dynamic_information_attention_number;  //动态关注数量
		int dynamic_information_forword_number;    //动态转发数量
		int dynamic_information_review_number;    //动态评论数量
		int dynamic_information_praise_number;   //动态点赞数量
		String nickname = session.getAttribute("nickname").toString();            //获取用户昵称
		String user_photo_path = session.getAttribute("user_photo_path").toString();     //获取用户头像url
		
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select = conn.prepareStatement(select_dynamic);
			ResultSet rs = ps_select.executeQuery();
			
			JSONArray my_dynamic_Array = new JSONArray();
			JSONObject jsonObj;
			JSONObject review_Obj;
			JSONArray review_Array;
			
			String select_dy_rev;     //查询评论的sql语句
			////////////////--------------------//////////////////////////
			while(rs.next()){
				jsonObj = new JSONObject();
				
				dynamic_photo_path = rs.getString("dynamic_photo_path");
				dynamic_information_content = rs.getString("dynamic_information_content");
				dynamic_information_date = rs.getDate("dynamic_information_date");
				dynamic_information_id = rs.getString("dynamic_information_id");
				dynamic_information_attention_number = rs.getInt("dynamic_information_attention_number");
				dynamic_information_forword_number = rs.getInt("dynamic_information_forword_number");
				dynamic_information_review_number = rs.getInt("dynamic_information_review_number");
				dynamic_information_praise_number = rs.getInt("dynamic_information_praise_number");
				
				jsonObj.put("dynamic_photo_path", dynamic_photo_path);
				jsonObj.put("dynamic_information_content", dynamic_information_content);
				jsonObj.put("dynamic_information_date", dynamic_information_date.toString());
				jsonObj.put("dynamic_information_id", dynamic_information_id);
				jsonObj.put("dynamic_information_attention_number", dynamic_information_attention_number);
				jsonObj.put("dynamic_information_forword_number", dynamic_information_forword_number);
				jsonObj.put("dynamic_information_review_number", dynamic_information_review_number);
				jsonObj.put("dynamic_information_praise_number", dynamic_information_praise_number);
				jsonObj.put("nickname", nickname);
				jsonObj.put("user_photo_path", user_photo_path);
				
				
				//------------------------//
                select_dy_rev = "select * from review where dynamic_information_id = '"+dynamic_information_id+"'";
				PreparedStatement review_sel = conn.prepareStatement(select_dy_rev);
				ResultSet review_rs = review_sel.executeQuery();
				review_Array = new JSONArray();
				while(review_rs.next()){
					review_Obj = new JSONObject();
					
					String reviewer_account = review_rs.getString("reviewer_account");  //评论者账户
					String review_content = review_rs.getString("review_content"); //评论类容
					String review_date = review_rs.getDate("review_date").toString();  //评论时间
					String review_id = review_rs.getString("review_id");     //评论编号
					
					review_Obj.put("review_id", review_id);
					review_Obj.put("review_date", review_date);
					review_Obj.put("reviewer_account", reviewer_account);
					review_Obj.put("review_content", review_content);
					
					String reviewer_nickname = "";
					String reviewer_photo = "";
					String select_revewer_nick = "select * from user_data where user_account = '"+reviewer_account+"'";
					PreparedStatement reviewer_nic = conn.prepareStatement(select_revewer_nick);
					ResultSet reviewer_nic_rs = reviewer_nic.executeQuery();
					if(reviewer_nic_rs.next()){
						reviewer_nickname = reviewer_nic_rs.getString("user_nickname");
						reviewer_photo = reviewer_nic_rs.getString("user_photo_path");
					}
					review_Obj.put("reviewer_nickname", reviewer_nickname);
					review_Obj.put("reviewer_photo", reviewer_photo);
					
					review_Array.put(review_Obj);
					
					reviewer_nic.close();
					reviewer_nic_rs.close();
				}
				//-----------------------------//
				jsonObj.put("review_Array", review_Array);
				my_dynamic_Array.put(jsonObj);
				
				review_sel.close();
				review_rs.close();
			}
			///////////////////////////////////////////////////////////////////------------//////////////
			
			out.print(my_dynamic_Array.toString());
			
			ps_select.close();
			rs.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}

}
